package cn.edu.lcu.cs.javaprogramming.clazz;

import cn.edu.lcu.cs.javaprogramming.util.Math;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import java.time.*;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * 日期时间演示
 *
 * @author ling
 * @date 2022/4/7 9:12
 */
public class DateDemo {

    @Test
    void date() {
        Date date = new Date(2000, 1, 1);
        System.out.println("date = " + date);

        Date now = new Date();
        System.out.println("now = " + now);
        System.out.println("now.getTime() = " + now.getTime());
    }

    /**
     * 生日计算。
     * 打印出生至今的年数、月数、天数
     *
     * @param year
     * @param month
     * @param day
     */
    @ParameterizedTest
    @CsvSource({"1949,10,1"})
    void birthdate(int year, int month, int day) {
        //代码填空
        LocalDate birthdate = LocalDate.of(year, month, day);
        System.out.println("birthdate = " + birthdate);

        LocalDate now = LocalDate.now();

        long years = birthdate.until(now, ChronoUnit.YEARS);
        System.out.println("years = " + years);

        long months = birthdate.until(now, ChronoUnit.MONTHS);
        System.out.println("months = " + months);

        long days = birthdate.until(now, ChronoUnit.DAYS);
        System.out.println("days = " + days);
    }

    /**
     * 时间计算。
     * 打印一个时间段耗费的纳秒数
     */
    @Test
    void duration() throws InterruptedException {
        Instant begin = Instant.now();
        // 调用一个比较耗时的操作
        //Math.factorialSafe(8100);
        Thread.sleep(2000);
        Instant end = Instant.now();
        Duration duration = Duration.between(begin, end);
        // 返回秒数及秒内纳秒数
        System.out.println("duration.getSeconds() = " + duration.getSeconds());
        System.out.println("duration.getNano() = " + duration.getNano());
        // 获取秒内纳秒数，把整秒数抹掉了。
        // 如果秒数为0，结果恰好是耗费的总纳秒数。
        System.out.println("duration.get(ChronoUnit.NANOS) = " + duration.get(ChronoUnit.NANOS));
        // 获取总纳秒的正确姿势
        System.out.println("ChronoUnit.NANOS.between(begin, end) = " + ChronoUnit.NANOS.between(begin, end));
        System.out.println("begin.until(end, ChronoUnit.NANOS) = " + begin.until(end, ChronoUnit.NANOS));
    }


    /**
     * 母亲节，5月的第2个星期日。
     */
    @Test
    void mothersDay() {
        LocalDate mothersDay = LocalDate.now();

        // 生成母亲节日期对象
        // 代码填空
        //LocalDate dayOfYear100 = mothersDay.withDayOfYear(100);
        //System.out.println("dayOfYear100 = " + dayOfYear100);

        mothersDay = mothersDay.withMonth(5).withDayOfMonth(1).with(DayOfWeek.SUNDAY).plusWeeks(1);

        System.out.println("mothersDay = " + mothersDay);
    }

    @Test
    void monthDay() {
        MonthDay childrensDay = MonthDay.of(6, 1);
        System.out.println("childrensDay = " + childrensDay);
    }

    /**
     * 带时区的日期时间
     */
    @Test
    void zonedDemo() {
        ZonedDateTime childrensDay2022 = ZonedDateTime.now().withMonth(6).withDayOfMonth(1);
        System.out.println("childrensDay = " + childrensDay2022);

        // 1900.6.1
        ZonedDateTime childrensDay1900 = childrensDay2022.withYear(1900);
        System.out.println("childrensDay1900 = " + childrensDay1900);

        // 1990.6.1中国采用夏令时
        ZonedDateTime childrensDay1990 = childrensDay2022.withYear(1990);
        System.out.println("childrensDay1990 = " + childrensDay1990);
    }

    /**
     * 毕业倒计时
     */
    @Test
    void graduationDay() {
        LocalDate graduationDay = LocalDate.of(2024, 6, 30);

        // 计算毕业日距今天的天数
        long days = 0;
        // 代码填空
        LocalDate now = LocalDate.now();
        // 以下两种方式均可
        days = now.until(graduationDay, ChronoUnit.DAYS);
        //days = ChronoUnit.DAYS.between(now, graduationDay);


        System.out.println("days = " + days);
    }


}
